package cn.iocoder.yudao.module.trade.controller.admin.depositorderreturn;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.depositorderreturn.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderreturn.DepositOrderReturnDO;
import cn.iocoder.yudao.module.trade.service.depositorderreturn.DepositOrderReturnService;

@Tag(name = "管理后台 - 押金单退瓶")
@RestController
@RequestMapping("/trade/deposit-order-return")
@Validated
public class DepositOrderReturnController {

    @Resource
    private DepositOrderReturnService depositOrderReturnService;

    @PostMapping("/create")
    @Operation(summary = "创建押金单退瓶")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:create')")
    public CommonResult<Long> createDepositOrderReturn(@Valid @RequestBody DepositOrderReturnSaveReqVO createReqVO) {
        return success(depositOrderReturnService.createDepositOrderReturn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新押金单退瓶")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:update')")
    public CommonResult<Boolean> updateDepositOrderReturn(@Valid @RequestBody DepositOrderReturnSaveReqVO updateReqVO) {
        depositOrderReturnService.updateDepositOrderReturn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除押金单退瓶")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:delete')")
    public CommonResult<Boolean> deleteDepositOrderReturn(@RequestParam("id") Long id) {
        depositOrderReturnService.deleteDepositOrderReturn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得押金单退瓶")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:query')")
    public CommonResult<DepositOrderReturnRespVO> getDepositOrderReturn(@RequestParam("id") Long id) {
        DepositOrderReturnRespVO depositOrderReturn = depositOrderReturnService.getDepositOrderReturn(id);
        return success(depositOrderReturn);
    }

    @GetMapping("/page")
    @Operation(summary = "获得押金单退瓶分页")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:query')")
    public CommonResult<PageResult<DepositOrderReturnRespVO>> getDepositOrderReturnPage(@Valid DepositOrderReturnPageReqVO pageReqVO) {
        PageResult<DepositOrderReturnRespVO> pageResult = depositOrderReturnService.getDepositOrderReturnPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出押金单退瓶 Excel")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-return:export')")
    @OperateLog(type = EXPORT)
    public void exportDepositOrderReturnExcel(@Valid DepositOrderReturnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepositOrderReturnDO> list = depositOrderReturnService.getDepositOrderReturnList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "押金单退瓶.xls", "数据", DepositOrderReturnRespVO.class,
                        BeanUtils.toBean(list, DepositOrderReturnRespVO.class));
    }

}